package com.ruoyi.sale.service.impl;

import java.util.List;

import com.ruoyi.base.domain.Customer;
import com.ruoyi.base.domain.GasBalance;
import com.ruoyi.base.mapper.CustomerMapper;
import com.ruoyi.base.mapper.GasBalanceMapper;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.sale.mapper.ReceiveRecordMapper;
import com.ruoyi.sale.domain.ReceiveRecord;
import com.ruoyi.sale.service.IReceiveRecordService;

/**
 * 收款记录
Service业务层处理
 *
 * @author h w
 * @date 2022-12-09
 */
@Service
public class ReceiveRecordServiceImpl implements IReceiveRecordService
{
    @Autowired
    private ReceiveRecordMapper receiveRecordMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private GasBalanceMapper gasBalanceMapper;

    /**
     * 查询收款记录

     *
     * @param receiveRecordId 收款记录
主键
     * @return 收款记录

     */
    @Override
    public ReceiveRecord selectReceiveRecordByReceiveRecordId(Long receiveRecordId)
    {
        return receiveRecordMapper.selectReceiveRecordByReceiveRecordId(receiveRecordId);
    }

    /**
     * 查询收款记录
列表
     *
     * @param receiveRecord 收款记录

     * @return 收款记录

     */
    @Override
    public List<ReceiveRecord> selectReceiveRecordList(ReceiveRecord receiveRecord)
    {
        return receiveRecordMapper.selectReceiveRecordList(receiveRecord);
    }

    /**
     * 新增收款记录

     *
     * @param receiveRecord 收款记录

     * @return 结果
     */
    @Override
    public int insertReceiveRecord(ReceiveRecord receiveRecord)
    {
        receiveRecord.setCreateTime(DateUtils.getNowDate());
        Customer paymentUnit = customerMapper.selectCustomerByCustomerId(Long.valueOf(receiveRecord.getPaymentUnit()));
        Customer receiveUnit = customerMapper.selectCustomerByCustomerId(Long.valueOf(receiveRecord.getReceiveUnit()));
        if(receiveUnit != null && paymentUnit != null) {
            GasBalance balance = new GasBalance();
            balance.setCustomerId(paymentUnit.getCustomerId());
            balance.setEnterpriseId(receiveUnit.getCustomerId());
            List<GasBalance> balanceList = gasBalanceMapper.selectGasBalanceList(balance);
            if(balanceList == null || balanceList.size() <= 0) {
                balance.setCustomerName(paymentUnit.getCustomerName());
                balance.setEnterpriseName(receiveUnit.getCustomerName());
                balance.setCustomerBalance(receiveRecord.getReceiveAmount());
                balance.setCreateBy(receiveRecord.getCreateBy());
                balance.setCreateTime(DateUtils.getNowDate());
                gasBalanceMapper.insertGasBalance(balance);
            } else {
                GasBalance balanceModify = balanceList.get(0);
                balanceModify.setCustomerBalance(balanceModify.getCustomerBalance().add(receiveRecord.getReceiveAmount()));
                balanceModify.setUpdateBy(receiveRecord.getCreateBy());
                balanceModify.setUpdateTime(DateUtils.getNowDate());
                gasBalanceMapper.updateGasBalance(balanceModify);
            }
            return receiveRecordMapper.insertReceiveRecord(receiveRecord);
        }


        return 0;
    }

    /**
     * 修改收款记录

     *
     * @param receiveRecord 收款记录

     * @return 结果
     */
    @Override
    public int updateReceiveRecord(ReceiveRecord receiveRecord)
    {
        receiveRecord.setUpdateTime(DateUtils.getNowDate());
        return receiveRecordMapper.updateReceiveRecord(receiveRecord);
    }

    /**
     * 批量删除收款记录

     *
     * @param receiveRecordIds 需要删除的收款记录
主键
     * @return 结果
     */
    @Override
    public int deleteReceiveRecordByReceiveRecordIds(Long[] receiveRecordIds)
    {
        return receiveRecordMapper.deleteReceiveRecordByReceiveRecordIds(receiveRecordIds);
    }

    /**
     * 删除收款记录
信息
     *
     * @param receiveRecordId 收款记录
主键
     * @return 结果
     */
    @Override
    public int deleteReceiveRecordByReceiveRecordId(Long receiveRecordId)
    {
        return receiveRecordMapper.deleteReceiveRecordByReceiveRecordId(receiveRecordId);
    }
}
